/**
 * Dirox Framework
 * Copyright (C) 2005 - 2007 Dirox / Synexser
 * 
 * All rights reserved.
 */

var emptyFunction = function(){};

function generateCatchException(object, func)
{
	on_func_error = ".On" + func.substring(0,1).toUpperCase() + func.substring(1) + "Error";

	command =  object + on_func_error + " = function(error) {";
	command += "alert('Error: \\n\\n' + error.message);";
	command += "return true;}\n";

	return command;
}

function callAJAX(object, func, args, callback)
{
	if (args != null && args['id'] != null) {
		ajax_running_indicator =  "<div style='text-align: center; width: 100%; margin-top: 0px;'><img src='";
		ajax_running_indicator += ajax_icon;
		ajax_running_indicator += "'></div>";
		$(args['id']).innerHTML = ajax_running_indicator;
	}

	callback = (callback == null) ? emptyFunction : callback;

	command =  generateCatchException(object,func);
	if (args != null) {
		command += object + "." + func + "(args, callback);";
	} else {
		command += object + "." + func + "(callback);";
	}

	eval(command);
}

function callAJAX_noloading(object, func, args, callback)
{
	callback = (callback == null) ? emptyFunction : callback;

	command =  generateCatchException(object,func);
	if (args != null) {
		command += object + "." + func + "(args, callback);";
	} else {
		command += object + "." + func + "(callback);";
	}

	eval(command);
}

function quickform_ajax_submit(object, func, form)
{
	command =  generateCatchException(object,func);
	command += object + "." + func + "(xoad.html.exportForm('" + form.id + "'), function(result) {";
	command += "alert(result);";
	command += "});";

	eval(command);
}

function paging_ajax_call(object,func,page)
{
	command =  generateCatchException(object,func);
	command += object + "." + func + "(page, function(result) {";
	command += "alert(result);";
	command += "});";

	eval(command);
	return false;
}

function isValidDateTime(datetimeStr)
{
	if (datetimeStr.indexOf(" ")<10){  // if year with 4 number
	dateStr = datetimeStr.substr(0, 6);
	dateStr+= '20'+datetimeStr.substr(6, 2);
	timeStr = datetimeStr.substr(9);} else { // if year with 2 number
	dateStr = datetimeStr.substr(0, 10);
	timeStr = datetimeStr.substr(11);
	}
	return isValidDate(dateStr) && isValidTime(timeStr);
}

function isValidDate(dateStr, format)
{
   dateArr = dateStr.split("/");
   if (isNaN(dateArr[2]) == true){return false;}
   else{
	   if (format == null) { format = "MDY"; }
	   format = format.toUpperCase();
	   if (format.length != 3) { format = "MDY"; }
	   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) ||
	      (format.indexOf("Y") == -1) ) { format = "MDY"; }
	   if (format.substring(0, 1) == "Y") { // If the year is first
	      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	   } else if (format.substring(1, 2) == "Y") { // If the year is second
	      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
	      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
	   } else { // The year must be third
	      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
	      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	   }
	   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
	   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
	   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
	   // Check to see if the 3 parts end up making a valid date
	   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else
	      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
	   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else
	      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
	   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else
	      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
	   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
	   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
	   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
	   if (parseFloat(dd) != dt.getDate()) { return false; }
	   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
	   return true;
   }
}

function isValidTime(timeStr){
	len = timeStr.length;
	i=0;count=0;
	if (len != 0 && len <= 8){
		temp = new Array();
		temp = timeStr.split(':');
		if (temp.length != 3){return false;}
		else{
			for(i = 0;i < temp.length-1;i++){
				if (isNaN(temp[i]) == true){return false;}
			}
		}
		if ((temp[0] >= 0 && temp[0] <= 23) && (temp[1] >= 0 && temp[1] <= 60) && (temp[2] >= 0 && temp[2] <= 60)){
			return true
		}else{return false;}
	}else{return false;}
}

function switchPanel(panels, selectedPanel)
{
	panels.each(function(item) {
		display = (item == selectedPanel) ? '' : 'none';
		if( $(item) != null )
		{
			$(item).style.display = display;
		}
	});
}

//function htmlspecialchars(str)
//{
//	str = str.replace(/&/g,'&amp;');
//	str = str.replace(/</g,'&lt;');
//	str = str.replace(/>/g,'&gt;');
//	str = str.replace(/"/g,'&quot;');
//	return str.replace(/'/g,'&#039;');
//}


function addslashesHTML(str)
{
	str = str.replace(/&#39;/g, '&#92;\'');
	return str;
}

function escape(str)
{
	str = str.replace(/&/g, '&amp;');
	str = str.replace(/</g, '&lt;');
	str = str.replace(/>/g, '&gt;');
	str = str.replace(/\"/g, '&quot;');
	str = str.replace(/\'/g, '&#39;');
	return str;
}

function is_int(variable) { 
	return(parseInt(variable) == variable); 
} 

function updateRealTime(){
		callAJAX("home_xoad_handler", "updateRealTime", $H({}), updateRealTime_callback);
	}
var updateRealTime_callback = function updateRealTime_callback(result){
		$('realtime').innerHTML = result;
}
function logout(){
	callAJAX("home_xoad_handler", "logout", $H({}), logout_callback);
}
var logout_callback = function logout_callback(result){
	location.href = application_url;
}

function showChangePasswordForm(){
	$('changepassword').style.display = "";	
}
function hideChangePasswordForm(){
	$('changepassword').style.display = "none";	
}
function changePassword(){
	var newpassword 		=  $('new password').value;
	var confirmpassword 	=  $('confirm password').value;
	if  (newpassword==confirmpassword)
	{
		callAJAX("home_xoad_handler", "changePassword", $H({newpassword: newpassword}), changePassword_callback);
	}
	else{
		$('changePassMessage').innerHTML = "Confirm password wrong!";
	}
}
var changePassword_callback = function changePassword_callback(result){
	$('changePassMessage').innerHTML = result;
}